package com.tuanzi.loan.business.repository.statistics.impl;

import com.tuanzi.loan.business.entity.statistics.StatNotRegisterLogin;
import com.tuanzi.loan.business.repository.statistics.custom.StatNotRegisterLoginRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.statistics.StatNotRegisterLoginRequest;
import com.tuanzi.loan.vo.response.statistics.StatNotRegisterLoginResponse;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.jooq.Record1;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.tables.CommProductPrdid.COMM_PRODUCT_PRDID;
import static com.tuanzi.loan.generated.jooq.loan.tables.CommonActivityRegisterRecord.COMMON_ACTIVITY_REGISTER_RECORD;
import static com.tuanzi.loan.generated.jooq.loan.tables.UserLoginRecord.USER_LOGIN_RECORD;
import static com.tuanzi.loan.generated.jooq.stat.Tables.STAT_NOT_REGISTER_LOGIN;

/**
 * User: liangZongXi
 * Create time: 14:00 2019/10/9
 * Description:
 */
public class StatNotRegisterLoginRepositoryImpl extends JOOQBaseRepository implements StatNotRegisterLoginRepositoryCustom {

    @Override
    protected TableImpl getTable() {
        return STAT_NOT_REGISTER_LOGIN;
    }

    @Override
    public Page<StatNotRegisterLoginResponse> list(StatNotRegisterLoginRequest request, Pageable pageable) {
        fields(STAT_NOT_REGISTER_LOGIN.fields());
        fields(COMM_PRODUCT_PRDID.PARENT_ID);
        leftJoin(COMM_PRODUCT_PRDID, STAT_NOT_REGISTER_LOGIN.PRDID.eq(COMM_PRODUCT_PRDID.PRDID));

        ge(STAT_NOT_REGISTER_LOGIN.S_DATE, request.getStartDate());
        le(STAT_NOT_REGISTER_LOGIN.S_DATE, request.getEndDate());
        eq(STAT_NOT_REGISTER_LOGIN.PRDID, request.getProductId());
        eq(COMM_PRODUCT_PRDID.PARENT_ID, request.getLargeProductId());
        orderBy(STAT_NOT_REGISTER_LOGIN.S_DATE, Sort.Direction.DESC);

        return search(pageable, record -> {
            StatNotRegisterLoginResponse registerLoginResponse = new StatNotRegisterLoginResponse();
            registerLoginResponse.setId(record.get(STAT_NOT_REGISTER_LOGIN.ID));
            registerLoginResponse.setStatDate(record.get(STAT_NOT_REGISTER_LOGIN.S_DATE));
            registerLoginResponse.setProductId(record.get(STAT_NOT_REGISTER_LOGIN.PRDID));
            registerLoginResponse.setLargeProductId(record.get(COMM_PRODUCT_PRDID.PARENT_ID));
            registerLoginResponse.setLoginNum(record.get(STAT_NOT_REGISTER_LOGIN.LOGIN_NUM));
            return registerLoginResponse;
        });
    }

    @Override
    public List<StatNotRegisterLogin> findYesterdayNotRegisterLogin() {
        LocalDate yesterday = LocalDate.now().plusDays(-1);
        Date bDate = new DateTime(yesterday.getYear(), yesterday.getMonthValue(), yesterday.getDayOfMonth(), 0, 0, 0).toDate();
        Date eDate = new DateTime(yesterday.getYear(), yesterday.getMonthValue(), yesterday.getDayOfMonth(), 23, 59, 59).toDate();

        SelectConditionStep<Record1<String>> registerCondition = dslContext.select(COMMON_ACTIVITY_REGISTER_RECORD.PHONE)
                .from(COMMON_ACTIVITY_REGISTER_RECORD)
                .where(COMMON_ACTIVITY_REGISTER_RECORD.CDATE.eq(new java.sql.Date(bDate.getTime())));

        return dslContext.select(DSL.countDistinct(USER_LOGIN_RECORD.ACCOUNT).as("LOGIN_NUM"), USER_LOGIN_RECORD.PRODUCTID,
                DSL.date(USER_LOGIN_RECORD.CTIME).as("S_DATE")).from(USER_LOGIN_RECORD)
                .where(USER_LOGIN_RECORD.CTIME.ge(new Timestamp(bDate.getTime()))
                        .and(USER_LOGIN_RECORD.CTIME.le(new Timestamp(eDate.getTime())))
                        .and(USER_LOGIN_RECORD.ACCOUNT.notIn(registerCondition))
                        .and(USER_LOGIN_RECORD.TYPE.eq(1)))
                .groupBy(USER_LOGIN_RECORD.PRODUCTID).fetch(record -> {
                    StatNotRegisterLogin  registerLogin = new StatNotRegisterLogin();
                    registerLogin.setStatDate((Date) record.get("S_DATE"));
                    Object loginNum = record.get("LOGIN_NUM");
                    registerLogin.setLoginNum(loginNum == null ? null: (Integer) loginNum);
                    String s = record.get(USER_LOGIN_RECORD.PRODUCTID);
                    registerLogin.setProductId(StringUtils.isBlank(s) ? null : Integer.valueOf(s));
                    return registerLogin;
                });
    }
}
